package com.jx.lcs.datareport.web;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;












import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;








import com.alibaba.druid.support.console.Option;
import com.jx.lcs.datareport.entity.EchartData;
import com.jx.lcs.datareport.entity.Series;
import com.jx.lcs.datareport.service.UserCityReportService;
import com.tianyu.jty.common.utils.DateUtils;
import com.tianyu.jty.common.web.BaseController;

/**
 * 城市统计
 * 
 * @date 2015年9月7日
 */

@Controller
@RequestMapping("userCityReport")
public class UserCityReportController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(UserCityReportController.class);

	@Autowired
	private UserCityReportService userCityReportService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "report/userCityReport";
	}

	/**
	 * 按城市统计B端用户----列表
	 */
	@RequestMapping(value = "list_Data", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> userCityReport() {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			List<Map<String, Object>> userData = userCityReportService.countUserByCity();
			System.out.print("userData="+userData);
			map.put("total", 0);
			map.put("rows", userData);
		
		} catch (Exception e) {
			logger.error("--按城市统计B端用户出现异常！", e);
		}

		return map;
	}
	/**
	 * 按城市统计B端用户---柱状图
	 * @return
	 */
	@RequestMapping(value = "line_Data", method = RequestMethod.POST)  
	@ResponseBody  
	public EchartData lineData() {  
		logger.info("lineData....");      
		List<String> category = new ArrayList<String>();//纵坐标
		List<Series> series = new ArrayList<Series>();//横坐标
		List<Map<String, Object>> userData = userCityReportService.countUserByCity();
		ArrayList<Integer> total = new ArrayList<Integer>();
		for(int i=userData.size()-1;i>-1;i--){
			Map cityMap = (Map)userData.get(i);
			category.add(cityMap.get("city")+"");
			total.add(Integer.parseInt(cityMap.get("total")+""));  
			series.add(new Series("", "line",total));
		}
		EchartData data=new EchartData(category, series);
		return data;
	}    

	/**
	 * 导出数据
	 */
	@RequestMapping(value = "export", method = RequestMethod.GET, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public void export(HttpServletResponse response) throws IOException {

		String fileName = "各城市理财师用户量数据-" + DateUtils.formatDate(DateUtils.getSysDate(), "yyyyMMdd");
		
		response.setContentType("text/html;charset=utf-8");
		response.setContentType("application/x-msdownload;");
		response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");

		userCityReportService.exportDataReport(response.getOutputStream());

	}

}
